package com.fate;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String addr;
    private Integer age;
    private BigDecimal score;
    private Date birth;
    private List<String> hobby;
    private static List<User> users = Arrays.asList(
            new User(1, "亚索", "巨神峰", 20, new BigDecimal(99), new Date(), Arrays.asList("吹风", "吹笛子", "睡觉")),
            new User(2, "德玛", "德玛西亚", 21, new BigDecimal(95), new Date(), Arrays.asList("大保健", "冲锋", "睡觉")),
            new User(3, "光辉", "德玛西亚", 22, new BigDecimal(80), new Date(), Arrays.asList("魔法棒", "激光", "睡觉")),
            new User(4, "发条", "黑色玫瑰", 23, new BigDecimal(75), new Date(), Arrays.asList("玩球", "上发条")),
            new User(5, "铁男", "黑色玫瑰", 24, new BigDecimal(75), new Date(), Arrays.asList("棒子", "小黑屋", "睡觉")),
            new User(6, "提莫", "巨神峰", 25, new BigDecimal(70), new Date(), Arrays.asList("种蘑菇", "采蘑菇", "吹笛子")),
            new User(7, "卢锡安", "巨神峰", 26, new BigDecimal(60), new Date(), Arrays.asList("双枪", "滑步")),
            new User(8, "卡牌大师", "诺克萨斯", 27, new BigDecimal(65), new Date(), Arrays.asList("玩牌", "黄牌")),
            new User(9, "雷克顿", "德玛西亚", 28, new BigDecimal(60), new Date(), Arrays.asList("飞顿", "睡觉"))
    );

    public static void main(String[] args) {


        //1. 拿到所有用户的名字集合  ---> List<String> 映射
        List<String> collect = users.parallelStream().map(User::getName).collect(Collectors.toList());
        System.out.println(collect);
        //2.拿到年龄小于25的人的名字集合
        List<String> collect1 = users.parallelStream().filter(user -> user.getAge() < 25).map(User::getName).collect(Collectors.toList());
        System.out.println(collect1);
//        //3.根据分数倒排序
        List<BigDecimal> collect2 = users.parallelStream().sorted(Comparator.comparing(User::getScore).reversed()).map(User::getScore).collect(Collectors.toList());
        System.out.println(collect2);
//        //4.拿到爱好里面有睡觉的 集合
        List<List<String>> collect3 = users.parallelStream().filter(user -> user.getHobby().contains("睡觉")).map(User::getHobby).collect(Collectors.toList());
        System.out.println(collect3);
//        //5.拿到Map<addr,List<com.fate.User>> //分组
        Map<String, List<User>> collect4 = users.parallelStream().collect(Collectors.groupingBy(User::getAddr));
        System.out.println(collect4);
//        //6.拿到来自德玛西亚的用户，根据分数排序，取前两个的  名字
        List<String> collect5 = users.parallelStream().filter(user -> user.getAddr().equals("德玛西亚"))
                .sorted(Comparator.comparing(User::getScore).reversed())
                .limit(2)
                .map(User::getName)
                .collect(Collectors.toList());
        System.out.println(collect5);
    }
}
